/**
 *
 * Bonita
 * Copyright (C) 1999 Bull S.A.
 * Bull 68 route de versailles  78434 Louveciennes Cedex France
 * Further information: bonita@objectweb.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 *
 * --------------------------------------------------------------------------
 * $Id: MailNotification.java,v 1.1 2007/10/20 20:19:16 larry Exp $
 *
 * --------------------------------------------------------------------------
 */

package hero.util;

import java.io.Serializable;

import javax.mail.Address;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Vector;

public final class MailNotification implements Serializable, java.lang.Cloneable {
    
    private Session session;
    public final static String CHARACTER_ENCODING = "UTF-8";
    
    public void initMailService() {
        try {
            BonitaServiceLocator serviceLocator = BonitaServiceLocator.getInstance();
            session = (Session) serviceLocator.getMailSession(BonitaServiceLocator.Services.MAIL_SERVICE);
        } catch (BonitaServiceException e) {
            e.printStackTrace();
        }
    }
    
    public void sendMail(String toAccount, String title,String body) throws Exception {
        try {
            this.initMailService();
            
            MimeMessage m = new MimeMessage(session);
            m.setFrom();
            Address[] to = new InternetAddress[] {new InternetAddress(toAccount) };
            m.setRecipients(javax.mail.Message.RecipientType.TO, to);
            m.setSubject(title);
            m.setSentDate(new java.util.Date());
            m.setContent(body,"text/plain;charset=" + CHARACTER_ENCODING);
            Transport.send(m);
        } catch (javax.mail.MessagingException e) {
            System.out.println("mail-service.xml configuration error: "+e);
        }
    }
    
    public void sendMail(Vector toAccount, String title,String body) throws Exception {
        try {
            this.initMailService();
            
            MimeMessage m = new MimeMessage(session);
            m.setFrom();
            for(int i=0; i<toAccount.size(); i++) {
                Address to = new InternetAddress((String)toAccount.elementAt(i));
                m.addRecipient(javax.mail.Message.RecipientType.TO, to);
            }
            m.setSubject(title);
            m.setSentDate(new java.util.Date());
            m.setContent(body,"text/plain;charset=" + CHARACTER_ENCODING);
            Transport.send(m);
        } catch (javax.mail.MessagingException e) {
            System.out.println("mail-service.xml configuration error: "+e);
        }
    }
    
    public MailNotification(){}
    
    public Object clone() throws java.lang.CloneNotSupportedException{
        return super.clone();
    }
    
}